unit xSimpleLog;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ShellAPI, Variants;

type
  TxSimpleLog = class(TComponent)
  private
    FFileName: string;
    FTimeStamp: Boolean;
    FLineChar: Char;
    FDivideChar: Char;
    FLineLength: Cardinal;
    procedure SetFileName(const Value: string);
    procedure SetTimeStamp(const Value: Boolean);
    procedure SetLineChar(const Value: Char);
    procedure SetDivideChar(const Value: Char);
    procedure SetLineLength(const Value: Cardinal);
    { Private declarations }
  protected
    { Protected declarations }
    FFileHandle: TextFile;
    procedure OpenLog; virtual;
    procedure CloseLog; virtual;
    function  GetTimeStamp: string; virtual;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    procedure Write(Msg: string); virtual;
    procedure Divide(Msg: string = ''; Ch: Char = #0); virtual;
    procedure Line(Ch: Char = #0); virtual;
    procedure Show; virtual;
    procedure Purge; virtual;
  published
    { Published declarations }
    property  FileName: string read FFileName write SetFileName stored True;
    property  TimeStamp: Boolean read FTimeStamp write SetTimeStamp default True;
    property  DivideChar: Char read FDivideChar write SetDivideChar default '-';
    property  LineChar: Char read FLineChar write SetLineChar default '=';
    property  LineLength: Cardinal read FLineLength write SetLineLength default 80;
  end;

procedure Register;

implementation

uses
  xConsts;

procedure Register;
begin
  RegisterComponents(SxVCLPalette, [TxSimpleLog]);
end;

{ TxSimpleLog }

procedure TxSimpleLog.CloseLog;
begin
  Flush(FFileHandle);
  CloseFile(FFileHandle);
end;

constructor TxSimpleLog.Create(AOwner: TComponent);
begin
  inherited;
  FFileName := 'TempLog.txt';
  FTimeStamp := True;
  FDivideChar := '-';
  FLineChar := '=';
  LineLength := 80;
end;

procedure TxSimpleLog.Divide(Msg: string; Ch: Char);
var
  i, j: Integer;
begin
  if (Ch = #0) then Ch := FDivideChar;

  i := Length(Msg);
  j := (FLineLength-i) div 2;
  Msg := StringOfChar(Ch, j)+Msg;
  Msg := Msg+StringOfChar(Ch, FLineLength-Length(Msg));
  Write(Msg);
end;

function TxSimpleLog.GetTimeStamp: string;
begin
  Result := DateTimeToStr(Now)+' : ';
end;

procedure TxSimpleLog.Line(Ch: Char);
begin
  if (Ch = #0) then Ch := FLineChar;
  Write(StringOfChar(Ch, FLineLength));
end;

procedure TxSimpleLog.OpenLog;
begin
  AssignFile(FFileHandle, FFileName);
  if FileExists(FFileName) then
    Append(FFileHandle)
  else
    Rewrite(FFileHandle);
end;

procedure TxSimpleLog.Purge;
begin
  AssignFile(FFileHandle, FFileName);
  if FileExists(FFileName) then
  begin
    Rewrite(FFileHandle);
    CloseFile(FFileHandle);
  end;
end;

procedure TxSimpleLog.SetDivideChar(const Value: Char);
begin
  FDivideChar := Value;
end;

procedure TxSimpleLog.SetFileName(const Value: string);
begin
  FFileName := Value;
end;

procedure TxSimpleLog.SetLineChar(const Value: Char);
begin
  FLineChar := Value;
end;

procedure TxSimpleLog.SetLineLength(const Value: Cardinal);
begin
  FLineLength := Value;
end;

procedure TxSimpleLog.SetTimeStamp(const Value: Boolean);
begin
  FTimeStamp := Value;
end;

procedure TxSimpleLog.Show;
begin
	ShellExecute(null, 'open', PChar(FFileName), '', '', SW_SHOWNORMAL);
end;

procedure TxSimpleLog.Write(Msg: string);
begin
  if FTimeStamp then
    Msg := GetTimeStamp + Msg;

  OpenLog;
  Writeln(FFileHandle, Msg);
  CloseLog;
end;

end.
